package com.bfly.mall.product.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.common.page.Pager;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.context.PagerThreadLocal;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.core.security.ActionModel;
import com.bfly.mall.product.dto.ProductSecKillFormDTO;
import com.bfly.mall.product.dto.ProductSecKillListDTO;
import com.bfly.mall.product.entity.ProductSecKill;
import com.bfly.mall.product.service.IProductSecKillService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品秒杀Controller
 *
 * @author 长沙商脉科技有限公司
 * @date 2021/2/16 11:19
 */
@RestController
@RequestMapping(value = "/manage/product/seckill")
public class ProductSecKillController extends BaseController {

    @Autowired
    private IProductSecKillService productSecKillService;

    /**
     * 秒杀产品列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2021/2/16 11:20
     */
    @GetMapping(value = "/list")
    public void list(HttpServletResponse response, HttpServletRequest request) {
        PagerThreadLocal.set(request);
        Map<String,Boolean> sortMap=new HashMap<>(2);
        sortMap.put("seq",true);
        sortMap.put("id",false);
        Pager<ProductSecKill> pager = productSecKillService.getPage(null,null,sortMap);
        Pager<ProductSecKillListDTO> dtoPager = new Pager<>(pager.getPageNo(), pager.getPageSize(), pager.getTotalCount());

        if (pager.getData() != null) {
            List<ProductSecKillListDTO> list = new ArrayList<>();
            for (ProductSecKill product : pager.getData()) {
                ProductSecKillListDTO dto = new ProductSecKillListDTO();
                BeanUtils.copyProperties(product, dto);
                list.add(dto);
            }
            dtoPager.setData(list);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dtoPager));
    }

    /**
     * 新增产品
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/24 12:35
     */
    @PostMapping(value = "/add")
    @ActionModel(value = "新增秒杀产品")
    public void addProduct(HttpServletResponse response, @Valid @RequestBody ProductSecKillFormDTO formDTO, BindingResult result) {
        validData(result);
        productSecKillService.save(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 编辑秒杀产品
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/24 12:36
     */
    @PostMapping(value = "/edit")
    @ActionModel(value = "编辑秒杀产品")
    public void editProduct(HttpServletResponse response, @Valid @RequestBody ProductSecKillFormDTO formDTO, BindingResult result) {
        validData(result);
        productSecKillService.edit(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 秒杀产品详情
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/24 12:11
     */
    @GetMapping(value = "/detail")
    public void detailProductSecKill(HttpServletResponse response, @RequestParam(value = "secKillId", defaultValue = "0") int secKillId) {
        ProductSecKill secKill = productSecKillService.get(secKillId);
        ProductSecKillFormDTO formDTO = new ProductSecKillFormDTO();
        BeanUtils.copyProperties(secKill, formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(formDTO));
    }

    /**
     * 删除秒杀产品
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/24 12:33
     */
    @PostMapping(value = "/delete")
    @ActionModel(value = "删除秒杀产品")
    public void delProductSecKill(HttpServletResponse response, @RequestBody Integer... secKillIds) {
        productSecKillService.remove(secKillIds);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 修改秒杀产品状态
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/31 9:36
     */
    @GetMapping(value = "/enabled")
    @ActionModel(value = "修改启用状态")
    public void enableProductSecKill(HttpServletResponse response, @RequestParam("secKillId") int secKillId, @RequestParam("enable") boolean enable) {
        ProductSecKill secKill = productSecKillService.get(secKillId);
        if (secKill == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的秒杀产品");
        }
        secKill.setEnabled(enable);
        productSecKillService.edit(secKill);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}